package br.ufrj.dcc.smarthotel.activities;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.Util;
import br.ufrj.dcc.smarthotel.controls.RequestedServicesControl;
import br.ufrj.dcc.smarthotel.entities.Request;
import br.ufrj.dcc.smarthotel.listeners.RequestedServicesClickListener;

public class RequestedServicesActivity extends AppActivity {
	private RequestedServicesControl control;
	private RequestedServicesClickListener listener;
	
	private List<Request> requestsList;
	
	private ListView listRequests;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.eventsmenu);
        
        this.findAllViews();
        this.control = new RequestedServicesControl(this);
        this.listener = new RequestedServicesClickListener(this);
        
        this.listRequests.setOnItemClickListener(this.listener);
        
        this.control.LoadRequests();
    }

	private void findAllViews() {
		this.listRequests = (ListView)this.findViewById(R.id.listRequests);
	}

	public void setRequestsList(List<Request> requestsList) {
		this.requestsList = requestsList;
		this.listRequests.setAdapter(new RequestsListAdapter(this.getApplicationContext(), R.id.listRequests, this.requestsList));
	}
	
	public List<Request> getRequestsList() {
		return this.requestsList;
	}
	
	class RequestsListAdapter extends ArrayAdapter<Request> {
		List<Request> requestsList;
		
		public RequestsListAdapter(Context context, int textViewResourceId, List<Request> objects) {
			super(context, textViewResourceId, objects);
			
			this.requestsList = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            
	            v = vi.inflate(R.layout.eventitem_list, null);
	        }
	        
	        Request request = this.requestsList.get(position);
	        
	        if (request != null) {
                TextView txtService = (TextView) v.findViewById(R.id.txtService);
                TextView txtRequestDate = (TextView) v.findViewById(R.id.txtRequestDate);
                TextView txtRegionName = (TextView) v.findViewById(R.id.txtRegionName);
                
                txtService.setText(request.getService().getName());
                txtRequestDate.setText(Util.getInstance(getContext()).writeDateTime(request.getRequestDate()));
                txtRegionName.setText(request.getRegionName());
	        }
	        return v;
		}	
	}
}
